//
//  NSMutableAttributedString+Extension.m
//  SynthesizeShoppingMall
//
//  Created by sanmi on 2018/1/18.
//  Copyright © 2018年 SanMiao. All rights reserved.
//

#import "NSMutableAttributedString+Extension.h"

@implementation NSMutableAttributedString (Extension)

+ (NSMutableAttributedString *)attributedAddImageName:(NSString *)imageName ContentStr:(NSString *)str ImageSize:(CGSize)size
{
    NSString * contentStr = [NSString stringWithFormat:@" %@",str];
    // 创建一个富文本
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    /**
     添加图片到指定的位置
     */
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    // 表情图片
    attchImage.image = [UIImage imageNamed:imageName];
    // 设置图片大小
    attchImage.bounds = CGRectMake(0,- size.height * 0.2, size.width, size.height);
    
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    [attriStr insertAttributedString:stringImage atIndex:0];
    
    return attriStr;
}

+ (NSMutableAttributedString *)attributedWithText:(NSString *)text rangeText:(NSString *)range rangeColor:(UIColor *)rangeColor rangeFont:(CGFloat)rangeFont
{
    if ([NSString isEmpty:range]) {
        
        range = @"";
    }
    //初始化富文本
    NSMutableAttributedString * hintString = [[NSMutableAttributedString alloc]initWithString:text];
    
    //获取要调整颜色的文字位置
    NSRange range1 = [[hintString string]rangeOfString:range];
    //调整颜色
    [hintString addAttribute:NSForegroundColorAttributeName value:rangeColor range:range1];
    //设置大小
    [hintString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:rangeFont] range:range1];
    
    
    return hintString;
}
@end
